import os
import pygame
import sys

os.environ['SDL_VIDEODRIVER'] = 'dummy'

# 初始化pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("键盘监听示例")
 
# 主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:  # 按键按下事件
            print(f"按键按下: {pygame.key.name(event.key)}")
            if event.key == pygame.K_ESCAPE:  # 按ESC键退出
                running = False
 
# 退出pygame
pygame.quit()
sys.exit()